<?php

/**
 * Abstract Model class
 * @author greenogre <ogre.green@gmail.com>
 * @since 17.05.2012
 * @copyright 2012. greenogre
 * @licence LGPL
 */
abstract class Green_ModelAbstract {

    /**
     * @var array
     */
    protected $_filters = array();

    /**
     * @abstract
     * @param $id
     * @return mixed
     */
    abstract public function get($id);

    /**
     * @abstract
     * @return mixed
     */
    abstract public function create();

    /**
     * @abstract
     * @param int $page
     * @param int $perPage
     * @return mixed
     */
    abstract public function getList($page=0, $perPage=0);

    /**
     * @param Green_FiltersAbstract $filters
     */
    public function setFilters(Green_FiltersAbstract $filters) {
        $this->_filters = $filters->toArray();
    }

    /**
     * @param $filterName
     * @param $filterValue
     */
    public function setFilter($filterName, $filterValue) {
        $this->_filters[$filterName] = $filterValue;
    }

    /**
     * apply filters.
     * example:
     *     filter Company
     *     for apply this filter create protected class method:
     *     protected function _filterCompany($value);
     *
     * @throws Green_Model_Exception
     */
    protected function _applyFilters() {

        if (is_array($this->_filters) == false) {
            throw new Green_Model_Exception('Model $this->_filters must be array');
        }

        foreach ($this->filters as $name => $value) {
            $methodName = '_filter' . $name;
            if (method_exists($this, $methodName)) {
                $this->$methodName($value);
            } else {
                throw new Green_Model_Exception("no apply-method _filter{$name} for filter {$name}");
            }
        }
    }

    /**
     * clear all data (filters and more)
     * between queries getList
     */
    protected function _reset() {
        $this->_filters = array();
    }


}